package persistence;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import model.Transactions;

public class TransactionsDAO extends BaseDAO {
	
	public TransactionsDAO() throws Exception{}
	
	public int deleteAllTransactions() throws SQLException{
		int retorno;
		String sql = "DELETE FROM transactions";
		PreparedStatement ps = this.getConnection().prepareStatement(sql);
		retorno = ps.executeUpdate();
		closeConnection();
		return retorno;  
	}
	
	public boolean insertTransactions(Transactions transactions, boolean closeConnection) throws SQLException{
		boolean retorno;
		String sql = "INSERT INTO transactions(" +
							"_id, " +
							"from_account_id, " +
							"to_account_id, " +
							"category_id, " +
							"project_id, " +
							"location_id, " +
							"note, " +
							"from_amount, " +
							"to_amount, " +
							"datetime, " +
							"provider, " +
							"accuracy, " +
							"latitude, " +
							"longitude, " +
							"is_template, " +
							"template_name, " +
							"recurrence, " +
							"status, " +
							"is_ccard_payment, " +
							"last_recurrence, " +
							"payee_id, " +
							"parent_id, " +
							"updated_on, " +
							"original_currency_id, " +
							"original_from_amount" +
							") values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		PreparedStatement ps = this.getConnection().prepareStatement(sql);
		ps.setInt	( 1, transactions.getId());
		ps.setInt	( 2, transactions.getFromAccountId());
		ps.setInt	( 3, transactions.getToAccountId());
		ps.setInt   ( 4, transactions.getCategoryId());
		ps.setInt   ( 5, transactions.getProjectId());
		ps.setInt   ( 6, transactions.getLocationId());
		ps.setString( 7, transactions.getNote());
		ps.setInt   ( 8, transactions.getFromAmount());
		ps.setInt   ( 9, transactions.getToAmount());
		ps.setLong  (10, transactions.getDatetime());
		ps.setString(11, transactions.getProvider());
		ps.setFloat	(12, transactions.getAccuracy());
		ps.setInt   (13, transactions.getLatitude());
		ps.setInt   (14, transactions.getLongitude());
		ps.setByte	(15, transactions.getIsTemplate());
		ps.setString(16, transactions.getTemplateName());
		ps.setString(17, transactions.getRecurrence());
		ps.setString(18, transactions.getStatus());
		ps.setByte	(19, transactions.getIsCcardPayment());
		ps.setLong	(20, transactions.getLastRecurrence());
		ps.setInt	(21, transactions.getPayeeId());
		ps.setInt	(22, transactions.getParentId());
		ps.setLong	(23, transactions.getUpdatedOn());
		ps.setInt	(24, transactions.getOriginalCurrencyId());
		ps.setInt	(25, transactions.getOriginalFromAmount());
		retorno = ps.executeUpdate() == 1;
	    if(closeConnection){
	    	closeConnection();
	    }
	    return retorno;
	}
	
	public int insertListTransactions(List<Transactions> listTransactions) throws Exception{
		int ret=0;
		for (int i = 0; i < listTransactions.size(); i++) {
			boolean foiInserido;
			foiInserido = insertTransactions(listTransactions.get(i),false);
			if(foiInserido){
				ret++;
			}
		}
		closeConnection();
		return ret;
	}
}
